package com.hkp.controller;

import com.hkp.pojo.College;
import com.hkp.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/college")
public class CollegeController {
    @Autowired
    private CollegeService collegeService;

    // 列表页
    @GetMapping("/list")
    public String list(Model model) {
        List<College> colleges = collegeService.list();
        model.addAttribute("colleges", colleges);
        return "college/list";
    }

    // 添加页面
    @GetMapping("/add")
    public String add() {
        return "college/add";
    }

    // 添加操作
    @PostMapping("/add")
    public String add(College college) {
        collegeService.save(college);
        return "redirect:/college/list";
    }

    // 编辑页面
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        College college = collegeService.getById(id);
        model.addAttribute("college", college);
        return "college/edit";
    }

    // 编辑操作
    @PostMapping("/edit")
    public String edit(College college) {
        collegeService.updateById(college);
        return "redirect:/college/list";
    }

    // 删除操作
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        collegeService.removeById(id);
        return "redirect:/college/list";
    }
}